package ying;/**
 * ClassName: ServletContextDemo02
 * Package: ying
 * Description:
 *
 * @Author 刘英杰
 * @Create 2024/9/28 21:12
 * @Version 1.0
 */

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * ServletContext中的方法
 *   InputStream getResourceAsStream():传入一个类的路径,然后返回一个资源的真实路径的输入流
 *
 */

@WebServlet("/context02")
public class ServletContextDemo02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext = getServletContext();
        InputStream is = servletContext.getResourceAsStream("/WEB-INF/classes/db.properties");

        Properties properties = new Properties();
        properties.load(is);
        System.out.println(properties);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
